#include "ConfigurationRootElement.h"

namespace OpenNano
{
    REGISTER_OBJECT_FACTORY( ConfigurationRootElement, CONFIG_TAG );

    ConfigurationRootElement::ConfigurationRootElement(TiXmlElement * element) :
                                    ConfigElement(element), logger(log4cplus::Logger::getInstance( CONFIG_TAG )),
                                     _version(0.1),
                                    _visual(0), _sim(0), _io(0),
                                    _visual_processed(false), _sim_processed(false), _io_processed(false)
    {
    }

    ConfigurationRootElement::~ConfigurationRootElement()
    {
    }

    void ConfigurationRootElement::init()
    {
        LOG4CPLUS_INFO(logger, "Configuration root element: initialized");
    }


    double ConfigurationRootElement::getVersion()
    {
        return _version;
    }

    void ConfigurationRootElement::attributeFound(TiXmlAttribute * attribute )
    {
        if (    attribute->NameTStr().compare(CONF_VERSION_ATTRIBUTE) == 0 &&
                attribute->QueryDoubleValue(&_version)==TIXML_SUCCESS )
        {
            _version = attribute->DoubleValue();  //useless?
        }
        else
        {
            //attribute not recognized
            LOG4CPLUS_WARN(logger, "Attribute not recognized:");
            LOG4CPLUS_WARN(logger, attribute->NameTStr());

            unknownAttributeFound(attribute);
        }
    }




    bool ConfigurationRootElement::postConfiguration()
    {
        return _visual_processed &&  _sim_processed && _io_processed;
    }

    void ConfigurationRootElement::textFound(const char * text)
    {
        LOG4CPLUS_WARN(logger, "Text not recognized:");
        LOG4CPLUS_WARN(logger, text);

        unknownTextFound(text);
    }





    void ConfigurationRootElement::childElementFound(TiXmlElement * childElement)
    {
        if ( childElement->ValueStr().compare(VISUALIZATION_TAG) == 0 )
        {

            _visual = (VisualizationElement *) ConfigElementFactory::getInstance().create(VISUALIZATION_TAG)(childElement);
            _visual_processed = _visual->process();
            if (_visual_processed)
            {
                ConfigMap::getInstance().insert(CM_Entry(VISUALIZATION_TAG, _visual));
            }
            else
            {
                delete _visual;
                LOG4CPLUS_ERROR(logger, "Visualization element not processed correctly");
            }
        }
        else if ( childElement->ValueStr().compare(SIMULATION_TAG) == 0 )
        {
            _sim = (SimulationElement *) ConfigElementFactory::getInstance().create(SIMULATION_TAG)(childElement);
            _sim_processed = _sim->process();
            if (_sim_processed)
            {
                ConfigMap::getInstance().insert(CM_Entry(SIMULATION_TAG, _sim));
            }
            else
            {
                delete _sim;
                LOG4CPLUS_ERROR(logger, "Simulation element not processed correctly");
            }
        }
        else if ( childElement->ValueStr().compare(INTERACTION_TAG) == 0 )
        {
            _io = (InteractionElement *) ConfigElementFactory::getInstance().create(INTERACTION_TAG)(childElement);
            _io_processed = _io->process();
            if (_io_processed)
            {
                ConfigMap::getInstance().insert(CM_Entry(INTERACTION_TAG, _io));
            }
            else
            {
                delete _io;
                LOG4CPLUS_ERROR(logger, "Interaction element not processed correctly");
            }
        }
        else
        {
            //Element not recognized
            LOG4CPLUS_WARN(logger, "Child element not recognized:");
            LOG4CPLUS_WARN(logger, childElement->ValueStr());

            unknownElementFound(childElement);
        }

        return;
    }
}
